package com.tcpudp.service;

import com.tcpudp.model.ServerConfig;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ServerManagerService {
    private final TcpServerManager tcpServerManager;
    private final UdpServerManager udpServerManager;

    public ServerManagerService(TcpServerManager tcpServerManager, UdpServerManager udpServerManager) {
        this.tcpServerManager = tcpServerManager;
        this.udpServerManager = udpServerManager;
    }

    public ServerConfig createServer(ServerConfig config) {
        if ("TCP".equals(config.getType())) {
            return tcpServerManager.createServer(config);
        } else if ("UDP".equals(config.getType())) {
            return udpServerManager.createServer(config);
        }
        return null;
    }

    public void startServer(String serverId) {
        ServerConfig config = getServer(serverId);
        if (config == null) {
            return;
        }

        if ("TCP".equals(config.getType())) {
            tcpServerManager.startServer(serverId);
        } else if ("UDP".equals(config.getType())) {
            udpServerManager.startServer(serverId);
        }
    }

    public void stopServer(String serverId) {
        ServerConfig config = getServer(serverId);
        if (config == null) {
            return;
        }

        if ("TCP".equals(config.getType())) {
            tcpServerManager.stopServer(serverId);
        } else if ("UDP".equals(config.getType())) {
            udpServerManager.stopServer(serverId);
        }
    }

    public void sendMessage(String connectionId, String message, String contentType) {
        // 根据连接ID找到对应的服务器类型
        ServerConfig config = null;
        for (ServerConfig tcpConfig : tcpServerManager.getAllServers().values()) {
            if (tcpConfig.isRunning()) {
                config = tcpConfig;
                break;
            }
        }

        if (config != null && "TCP".equals(config.getType())) {
            tcpServerManager.sendMessage(connectionId, message, contentType);
        } else {
            udpServerManager.sendMessage(connectionId, message, contentType);
        }
    }

    public ServerConfig getServer(String serverId) {
        ServerConfig config = tcpServerManager.getServer(serverId);
        if (config != null) {
            return config;
        }
        return udpServerManager.getServer(serverId);
    }

    public List<ServerConfig> getAllServers() {
        List<ServerConfig> allServers = new ArrayList<>();
        
        Map<String, ServerConfig> tcpServers = tcpServerManager.getAllServers();
        Map<String, ServerConfig> udpServers = udpServerManager.getAllServers();
        
        allServers.addAll(tcpServers.values());
        allServers.addAll(udpServers.values());
        
        return allServers;
    }

    public void updateServer(ServerConfig config) {
        if ("TCP".equals(config.getType())) {
            tcpServerManager.updateServer(config);
        } else if ("UDP".equals(config.getType())) {
            udpServerManager.updateServer(config);
        }
    }

    public void deleteServer(String serverId) {
        ServerConfig config = getServer(serverId);
        if (config == null) {
            return;
        }

        if ("TCP".equals(config.getType())) {
            tcpServerManager.deleteServer(serverId);
        } else if ("UDP".equals(config.getType())) {
            udpServerManager.deleteServer(serverId);
        }
    }
}